/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-01-08
*/
package com.rzico.account.controller;

import com.rzico.account.entity.PhonePayment;
import com.rzico.account.service.PhonePaymentService;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.core.entity.SysMch;
import com.rzico.core.entity.SysPlugin;
import com.rzico.core.plugin.PaymentPlugin;
import com.rzico.core.service.SysMchService;
import com.rzico.core.service.SysPluginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 豆讯话费支付控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2021-01-08
 */
@Api(description = "豆讯话费订单支付接口")
@RestController
@RequestMapping("/phonePayment")
public class PhonePaymentController extends BaseController {

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SysPluginService sysPluginService;

    @Autowired
    private SysMchService sysMchService;

    @Autowired
    private PhonePaymentService phonePaymentService;

    /**
     *  状态查询
     *
     * @param sn
     * @return
     */
    @Log(desc = "状态查询", type = Log.LOG_TYPE.SELECT)
    @ApiOperation("状态查询")
    @GetMapping("/query")
    public CommResult<PhonePayment> query(String sn, HttpServletRequest request) {

        PhonePayment phonePayment = phonePaymentService.findBySn(sn);

        PaymentPlugin paymentPlugin = sysPluginService.getPaymentPlugin(phonePayment.getPaymentPluginId());

        Enterprise enterprise = enterpriseService.findById(phonePayment.getEnterpriseId());

        SysMch sysMch = sysMchService.selectByPrimaryKey(enterprise.getMchId());
        if (sysMch.getMchType().equals(2)) {
            //特约商户,服务商代收
            enterprise = enterpriseService.findByMch(sysMch.getIsvId());
        }

        SysPlugin sysPlugin = sysPluginService.findByPlugin(enterprise.getMchId(),phonePayment.getPaymentPluginId());
        if (sysPlugin==null) {
            return CommResult.error("无效插件编号");
        }

        Map<String,Object> resultMap = paymentPlugin.tradeQuery(sysPlugin,phonePayment.getSn(),phonePayment.getTranSn(),request);

        if ("0000".equals(resultMap.get("return_code").toString())) {
            try {
                phonePaymentService.handle(sn,resultMap.get("tranSn").toString());
                return CommResult.success(resultMap);
            } catch (Exception e) {
                return CommResult.error("通知失败");
            }
        } else if ("0001".equals(resultMap.get("return_code").toString())) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return query(sn,request);
        } else {
            return CommResult.success(resultMap);
        }

    }

}
